package com.etc.controller;

import com.etc.entity.Hospital;
import com.etc.entity.Notice;
import com.etc.service.NoticeService;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/add.do")
    public String noticeAdd(@Param("notice_title")String notice_title, @Param("notice_content")String notice_content){
        noticeService.noticeAdd(notice_title,notice_content);
        return "redirect:/notice/queryAll.do/1";
    }
    @RequestMapping("/delete.do")
    public String noticeDelete(int id){
        noticeService.noticeDelete(id);
        return "redirect:/notice/queryAll.do/1";
    }
    @RequestMapping("/query1.do")
    @ResponseBody
    public Notice noticeQueryById(@RequestBody Notice notice){
        Notice notice1 = noticeService.noticeQueryById(notice.getNotice_id());
        return notice1;
    }
    @RequestMapping("/query10.do")
    @ResponseBody
    public List<Notice> queryTen(){
        List<Notice> notices1 = noticeService.queryTen();
        System.out.println(notices1);
        return notices1;
    }
    @RequestMapping("/queryAll.do")
    public String queryAll(Model model){
        List<Notice> notices = noticeService.queryAll();
        int pagesize=5;
        int counttotal=noticeService.queryAll().size();
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=(counttotal-1)/pagesize+1;
        }
        model.addAttribute("notices",notices);
        model.addAttribute("pages",pages);
        model.addAttribute("pagecurrent",1);
        model.addAttribute("counttotal",counttotal);
        model.addAttribute("pagesize",pagesize);
        return "Notice";
    }
    @RequestMapping("/modify.do")
    public String modify(@Param("notice_title")String notice_title, @Param("notice_content")String notice_content,@Param("notice_id")int notice_id){
        noticeService.modify(notice_title,notice_content,notice_id);
        return "redirect:/notice/queryAll.do/1";
    }
    @RequestMapping("/queryByTitle.do")
    public String queryByTitle(String title,Model model){
        List<Notice> notices = noticeService.queryByTitle(title);
        int pagesize=5;
        int counttotal=noticeService.queryByTitle(title).size();
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=(counttotal-1)/pagesize+1;
        }
        model.addAttribute("notices",notices);
        model.addAttribute("pages",pages);
        model.addAttribute("pagecurrent",1);
        model.addAttribute("counttotal",counttotal);
        model.addAttribute("pagesize",pagesize);
        return "Notice";
    }
    @RequestMapping(value = "/queryAll.do/{pagenum}",method = RequestMethod.GET)
    public String noticesmanager(Model model, @PathVariable(value = "pagenum",required = false) String pagenum){
        System.out.println("进入方法"+pagenum);
        //每页长度
        int pagesize=5;
        //当前页
        int pagecurrent=1;
        //查询数据库总数据
        int counttotal=noticeService.queryAll().size();
        //判断总页数
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=(counttotal-1)/pagesize+1;
        }
        //判断当前页
        if(pagenum==null){
            pagecurrent=1;
        }else {
            pagecurrent=Integer.valueOf(pagenum);
        }
        //首页每页判断
        if(pagecurrent<=1){
            pagecurrent=1;
        }else if(pagecurrent>=pages){
            pagecurrent=pages;
        }
        //分页start
        int pagestart=0;
        pagestart=(pagecurrent-1)*pagesize;




        //根据当前页和页面尺度进行分页
        PageHelper.startPage(pagecurrent,pagesize);
        List<Notice> notices = noticeService.queryAll();
        //加入公告列表信息
        model.addAttribute("notices",notices);
        //加入总页数
        model.addAttribute("pages",pages);
        //加入当前页
        model.addAttribute("pagecurrent",pagecurrent);
        //加入查询总条数
        model.addAttribute("counttotal",counttotal);
        //加入每页宽度
        model.addAttribute("pagesize",pagesize);
        //返回页面
        return "Notice";
    }
}
